<template>
  <a-modal
    :title="`编辑工资`"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="ID" v-show="false">
          <a-input v-decorator="['id']" name="id" />
        </a-form-item>
        <a-form-item label="基本工资">
          <a-input v-decorator="['basicWages', { initialValue: '0' }]" prefix="￥" placeholder="请输入基本工资" name="basicWages" />
        </a-form-item>
        <a-form-item label="商品提成">
          <a-input
            v-decorator="['phoneWage', { initialValue: '0' }]"
            prefix="￥"
            :disabled="sp"
            placeholder="请输入商品提成"
            name="phoneWage"
          >
            <template #addonAfter>
              <a-button type="primary" v-if="sp" @click="handleSP()">手动填写</a-button>
              <a-button type="danger" v-else @click="handleSP()">自动计算</a-button>
            </template>
          </a-input>
        </a-form-item>
        <a-form-item label="运营商提成">
          <a-input
            v-decorator="['operWage', { initialValue: '0' }]"
            prefix="￥"
            :disabled="yys" 
            placeholder="请输入运营商提成"
            name="operWage"
          >
            <template #addonAfter>
              <div style="padding: 0">
                <a-button type="primary" v-if="yys" @click="handleYYS()">手动填写</a-button>
                <a-button type="danger" v-else @click="handleYYS()">自动计算</a-button>
              </div>
            </template>
          </a-input>
        </a-form-item>
        <!-- <a-form-item label="AA">
          <a-input v-decorator="['AA', { initialValue: '0' }]" prefix="￥" :disabled="AA" placeholder="请输入AA" name="AA">
            <template #addonAfter>
              <a-button type="primary" v-if="AA" @click="handleAA()">手动填写</a-button>
              <a-button type="danger" v-else @click="handleAA()">自动计算</a-button>
            </template>
          </a-input>
        </a-form-item> -->
        <a-form-item label="实发工资">
          <a-input v-decorator="['finalWages', { initialValue: '0' }]" prefix="￥" placeholder="请输入实发工资" name="finalWages" />
        </a-form-item>
        <a-form-item label="备注">
          <a-input v-decorator="['memo']" name="memo" placeholder="请输入备注" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
  
  <script>
import pick from 'lodash.pick'
// 表单字段
const fields = ['basicWages','phoneWage','operWage', 'finalWages','memo','id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      sp: true,
      yys: true,
      AA: true,
      form: this.$form.createForm(this),
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    handleSP() {
      if(!this.sp){
        const data = this.form.getFieldsValue()
        this.form.setFieldsValue({finalWages:Number(data.phoneWage) + Number(data.basicWages) + Number(data.operWage)})
      }
      this.sp = !this.sp
    },
    handleAA() {
      this.AA = !this.AA
    },
    handleYYS() {
      if(!this.yys){
        const data = this.form.getFieldsValue()
        this.form.setFieldsValue({finalWages:Number(data.phoneWage) + Number(data.basicWages) + Number(data.operWage)})
      }
      this.yys = !this.yys
    },
  },
}
</script>
<style>
.ant-input-group-addon{
  padding: 0;
}
</style>